/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.close.blacklist.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.hopes.bizz.close.blacklist.dto.ClosedBlacklistPersonExcelDTO;
import com.hopes.bizz.close.blacklist.entity.ClosedBlacklistCarEntity;
import com.hopes.bizz.close.blacklist.dto.ClosedBlacklistCarExcelDTO;
import com.hopes.bizz.close.blacklist.vo.ClosedBlacklistCarExcelVO;
import com.hopes.bizz.close.blacklist.mapper.ClosedBlacklistCarMapper;
import com.hopes.bizz.close.blacklist.service.ClosedBlacklistCarService;
import com.hopes.bizz.common.core.constant.CommonConstant;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.enums.AdminDictEnum;
import com.hopes.boss.admin.api.resolver.enums.SysDictEnum;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
 * 车辆黑名单
 *
 * @author pig
 * @date 2024-01-29 18:40:55
 */
@Service
public class ClosedBlacklistCarServiceImpl extends ServiceImpl<ClosedBlacklistCarMapper, ClosedBlacklistCarEntity> implements ClosedBlacklistCarService {

    @Override
    public boolean save(ClosedBlacklistCarEntity closedBlacklistCar) {
        closedBlacklistCar.setSyncFlag(CommonConstant.boolean_false_string);
        return SqlHelper.retBool(getBaseMapper().insert(closedBlacklistCar));
    }

    @Override
    public ValidateImportBO<ClosedBlacklistCarExcelDTO> validateImport(List<ClosedBlacklistCarExcelDTO> excelDTOList) {
        ValidateImportBO<ClosedBlacklistCarExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<ClosedBlacklistCarExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;

        for (ClosedBlacklistCarExcelDTO closedBlacklistCarExcelDTO : excelDTOList) {
            final String validateRes = ValidatorUtils.importValidateEntity(closedBlacklistCarExcelDTO);
            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(closedBlacklistCarExcelDTO);
            } else {
                failedNum++;
                errorMessageList.add(new ErrorMessage(closedBlacklistCarExcelDTO.getLineNum(), new HashSet<>(){
                    private static final long serialVersionUID = 1544007667186208650L;
                    {
                        add(validateRes);
                    }
                }));
            }
        }

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public ClosedBlacklistCarEntity importDtoToEntity(ClosedBlacklistCarExcelDTO excelDto) {
        ClosedBlacklistCarEntity entity= new ClosedBlacklistCarEntity();
        BeanUtils.copyProperties(excelDto, entity);
        if (StrUtil.isNotEmpty(excelDto.getOrgIdStr())) {
            entity.setOrgId(DictResolver.getItemValueByLabel(AdminDictEnum.sys_company, excelDto.getOrgIdStr(), Long::valueOf));
        }
        entity.setSyncFlag(CommonConstant.boolean_false_string);
        return entity;
    }

    @Override
    public ClosedBlacklistCarExcelVO exportEntityToVo(ClosedBlacklistCarEntity entity) {
        ClosedBlacklistCarExcelVO excelVO = new ClosedBlacklistCarExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        excelVO.setOrgIdStr(DictResolver.getItemLabelByValue(AdminDictEnum.sys_company, entity.getOrgId()));
        excelVO.setSyncFlagStr(DictResolver.getItemLabelByValue(SysDictEnum.yes_no_type, entity.getSyncFlag()));
        return excelVO;
    }
}
